package slaverDown;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;

import tool.GetProper;
import tool.MysqlApi;

import dataStruct.DataStoreTask;
import dataStruct.DataStoreTaskBag;
import dataStruct.EnumHeartBeatType;
import dataStruct.FtpFileDowndedCount;
import dataStruct.FtpServerInfo;

public class SlaverDown {
	public SlaverDown(Queue<String> dataStoreTaskSlaverChunkQueue,
			Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap, 
			Map<String,  FtpFileDowndedCount> ftpFileDownedSizeMap)
	{
		masterProper = GetProper.get_properties("conf/config.ini");
		SlaverDown.ftpServerInfo = new FtpServerInfo(masterProper);
		
		SlaverDown.dataStoreTaskSlaverChunkMap =  dataStoreTaskSlaverChunkMap;
		SlaverDown.dataStoreTaskSlaverChunkQueue =   dataStoreTaskSlaverChunkQueue;
		SlaverDown.ftpFileDownedSizeMap =  ftpFileDownedSizeMap;
	}
	public Boolean move_chunk(Integer slaverId) throws SQLException
	{
		Statement state = MysqlApi.create_statement();
		String selectChunkOnSlaver = String.format("select chunk_id from slaver_chunk where slaver_id = '%s'", slaverId);
		ResultSet chunkSet = state.executeQuery(selectChunkOnSlaver);
		
		Statement state1 = MysqlApi.create_statement();
		while(chunkSet.next())
		{
			String chunkId = chunkSet.getString(1);
			String selectSlaverHasChunk = String.format("select slaver_id, slaver_chunk_id from slaver_chunk where chunk_id = '%s'", chunkId);

			ResultSet slaverSet = state1.executeQuery(selectSlaverHasChunk);
			while(slaverSet.next())
			{
				Integer slaverIdBackup = slaverSet.getInt(1);
				if(slaverId.equals(slaverIdBackup) == false)
				{
					DataStoreTask dataStoreTask = new DataStoreTask();
					dataStoreTask.chunkId =chunkId;
					dataStoreTask.heartBeatType = EnumHeartBeatType.RESPONSE_UPLOAD_FILE;
					FtpServerInfo ftpInfo = new FtpServerInfo(ftpServerInfo);
					ftpInfo.ftpServerPath += "/" + chunkId;
					dataStoreTask.ftpServerInfo = ftpInfo;
					
					Statement state2 = MysqlApi.create_statement();
					String selectChunkInfo = String.format("select chunk_offset, chunk_size from chunk_info where chunk_id='%s'", chunkId);
					ResultSet chunkInfoSet = state2.executeQuery(selectChunkInfo );
					if(chunkInfoSet.next())
					{
						dataStoreTask.offset = chunkInfoSet.getLong(1);
						dataStoreTask.size = chunkInfoSet.getLong(2);
					}
					else
					{
						return false;
					}
					dataStoreTask.totalSize = dataStoreTask.size;
					
					DataStoreTaskBag dataStoreTaskBag = new DataStoreTaskBag();
					dataStoreTaskBag.dataStoreTask = dataStoreTask;
					dataStoreTaskBag.slaverId = slaverIdBackup;
					dataStoreTaskBag.startTime = new Long(0);
					dataStoreTaskBag.isSlaverDown = true;
					
				     FtpFileDowndedCount ftpFileDowndedCount = new FtpFileDowndedCount();
				     ftpFileDowndedCount.remainSize = dataStoreTask.size;
					ftpFileDownedSizeMap.put(dataStoreTask.ftpServerInfo.ftpServerPath, ftpFileDowndedCount);
					
					dataStoreTaskSlaverChunkMap.put(slaverSet.getString(2), dataStoreTaskBag);
					dataStoreTaskSlaverChunkQueue.add(slaverSet.getString(2));
					
					break;
				}
			}
		}
		return true;
	}
	private static Properties masterProper;
	private static FtpServerInfo ftpServerInfo ;
	private static	Queue<String> dataStoreTaskSlaverChunkQueue;
	private static Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap;
	private static Map<String,  FtpFileDowndedCount> ftpFileDownedSizeMap;
}
